# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function(cc_test_build TARGET_NAME)
  if(WITH_TESTING)
    set(oneValueArgs "")
    set(multiValueArgs SRCS DEPS)
    cmake_parse_arguments(cc_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    add_executable(${TARGET_NAME} ${cc_test_SRCS})
    set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests/bin)
    if(WIN32)
      if("${cc_test_DEPS};" MATCHES "python;")
        list(REMOVE_ITEM cc_test_DEPS python)
        target_link_libraries(${TARGET_NAME} PUBLIC ${PYTHON_LIBRARIES})
      endif()
      set(EXTERNAL_LIB "")
    elseif(APPLE)
      set(EXTERNAL_LIB "-ldl -lpthread")
    else()
      set(EXTERNAL_LIB "-lrt -ldl -lpthread")
    endif()
    get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES)
    target_link_libraries(${TARGET_NAME} PUBLIC ${cc_test_DEPS} ${os_dependency_modules} fastdeploy_gtest_main gtest glog ${EXTERNAL_LIB})
    add_dependencies(${TARGET_NAME} ${cc_test_DEPS} gtest)
  endif()
endfunction()

function(cc_test TARGET_NAME)
  if(WITH_TESTING)
    set(oneValueArgs "")
    set(multiValueArgs SRCS DEPS ARGS)
    cmake_parse_arguments(cc_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    cc_test_build(${TARGET_NAME}
      SRCS ${cc_test_SRCS}
      DEPS ${cc_test_DEPS})
    add_test(NAME ${TARGET_NAME} COMMAND ${CMAKE_COMMAND})
  endif()
endfunction(cc_test)

function(add_fastdeploy_unittest CC_FILE)
  set(TEMP_TARGET_FILE ${CC_FILE})
  string(REGEX MATCHALL "[0-9A-Za-z_]*.cc" FILE_NAME ${CC_FILE})
  string(REGEX REPLACE ".cc" "" FILE_PREFIX ${FILE_NAME})
  set(TEMP_TARGET_NAME ${FILE_PREFIX})
  if (EXISTS ${TEMP_TARGET_FILE} AND TARGET fastdeploy)
    cc_test(${TEMP_TARGET_NAME} SRCS ${TEMP_TARGET_FILE} DEPS fastdeploy)
    message(STATUS "  Added FastDeploy unittest       : ${TEMP_TARGET_NAME}")
  endif()
  unset(TEMP_TARGET_FILE)
  unset(TEMP_TARGET_NAME)
endfunction()

if(WITH_TESTING)
  if(ANDROID OR IOS)
    # gtest in FastDeploy is not support for cross compiling now.
    message(FATAL_ERROR "Not support unittest for Android and IOS now.")
  endif()
  include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  add_library(fastdeploy_gtest_main STATIC gtest_main)
  target_link_libraries(fastdeploy_gtest_main PUBLIC gtest gflags)
  message(STATUS "")
  message(STATUS "*************FastDeploy Unittest Summary**********")
  file(GLOB_RECURSE ALL_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/*/test_*.cc)

  file(GLOB_RECURSE TIMVX_SRCS ${PROJECT_SOURCE_DIR}/tests/timvx/test_*.cc)
  list(REMOVE_ITEM ALL_TEST_SRCS ${TIMVX_SRCS})
  if(NOT ENABLE_VISION)
    # vision_preprocess and release_task need vision
    file(GLOB_RECURSE VISION_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/vision_preprocess/test_*.cc)
    file(GLOB_RECURSE RELEASE_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/release_task/test_*.cc)
    list(REMOVE_ITEM ALL_TEST_SRCS ${VISION_TEST_SRCS} ${RELEASE_TEST_SRCS})
  endif()
  foreach(_CC_FILE ${ALL_TEST_SRCS})
    add_fastdeploy_unittest(${_CC_FILE})
  endforeach()
endif()
